<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-row>
                <el-col>
                    <el-button type="primary" @click="showAddDialog">添加分类</el-button>
                </el-col>
            </el-row>
            <!-- 使用第三方插件vue-table-width-tree-grid 展示数据-->
            <tree-table
                :data="cateList"
                :columns="columns"
                :selection-type="false"
                show-index
                index-text="#"
                border
                :show-row-hover="false"
                :expand-type="false"
                class="treeTable"
            >
                <template slot="isok" slot-scope="scope">
                    <i
                        class="el-icon-success"
                        v-if="scope.row.cat_deleted === false"
                        style="color:green"
                    ></i>
                    <i class="el-icon-error" v-else style="color:red"></i>
                </template>
                <template slot="order" slot-scope="scope">
                    <el-tag size="mini" v-if="scope.row.cat_level===0">一级</el-tag>
                    <el-tag size="mini" type="success" v-else-if="scope.row.cat_level===1">二级</el-tag>
                    <el-tag size="mini" type="warning" v-else>三级</el-tag>
                </template>
                <template slot="caozuo" slot-scope="scope">
                    <el-button size="mini" type="primary" icon="el-icon-edit" @click="showEditDialog">编辑</el-button>
                    <el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteCate">删除</el-button>
                </template>
            </tree-table>
            <!-- 分页 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
            ></el-pagination>
        </el-card>
        <!-- 添加分类按钮弹出框 -->
        <el-dialog
            title="添加分类"
            :visible.sync="addCateDialogVisible"
            width="50%"
            @close="addCateDialogClosed"
        >
            <el-form
                :model="addCateForm"
                :rules="addCateFormRules"
                ref="addCateFormRef"
                label-width="100px"
            >
                <el-form-item label="分类名称" prop="cat_name">
                    <el-input v-model="addCateForm.cat_name"></el-input>
                </el-form-item>
                <el-form-item label="父级分类" prop="cat_pid">
                    <!-- 级联选择器 -->
                    <el-cascader
                        v-model="selectKeys"
                        :options="parentCateList"
                        :props="cascaderProps"
                        @change="parentSelectChange"
                        clearable
                    ></el-cascader>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addCateDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addCate">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            cateList: [],
            columns: [
                {
                    label: '分类名称',
                    prop: 'cat_name'
                },
                {
                    label: '是否有效',
                    //prop: 'cat_deleted',
                    type: 'template',
                    template: 'isok'
                },
                {
                    label: '排序',
                    prop: '',
                    type: 'template',
                    template: 'order'
                },
                {
                    label: '操作',
                    prop: '',
                    type: 'template',
                    template: 'caozuo'
                }
            ],
            queryInfo: {
                type: 3,
                pagenum: 1,
                pagesize: 5
            },
            total: 0,
            addCateDialogVisible: false,
            addCateForm: {
                cat_name: '',
                cat_pid: 0,
                cat_level: 0
            },
            addCateFormRules: {
                cat_name: [
                    {
                        required: true,
                        message: '请输入分类名称',
                        trigger: 'blur'
                    }
                ]
            },
            selectKeys: [],
            //父级列表 只获取到1，2级的
            parentCateList: [],
            cascaderProps: {
                value: 'cat_id',
                label: 'cat_name',
                children: 'children',
                expandTrigger: 'hover'
            }
        }
    },
    created() {
        this.getCateList()
    },
    methods: {
        async getCateList() {
            const { data: res } = await this.$http.get('categories', {
                params: this.queryInfo
            })
            if (res.meta.status !== 200) {
                return this.$message.error('获取商品列表数据失败')
            }
            //console.log(res.data)
            //将数据列表赋值给cateList
            this.cateList = res.data.result
            this.total = res.data.total
        },
        handleSizeChange(value) {
            this.queryInfo.pagesize = value
            this.getCateList()
        },
        handleCurrentChange(value) {
            this.queryInfo.pagenum = value
            this.getCateList()
        },
        showAddDialog() {
            //获取父级列表
            this.getParentCateList()
            this.addCateDialogVisible = true
        },
        async getParentCateList() {
            const { data: res } = await this.$http.get('categories', {
                params: { type: 2 }
            })
            if (res.meta.status !== 200) {
                return this.$message.error('获取商品分类列表数据失败')
            }
            this.parentCateList = res.data
        },
        //添加分类
        addCate() {
            this.$refs.addCateFormRef.validate(async (valid) => {
                if (!valid) {
                    return this.$message.error('请填写规范信息')
                }
                const { data: res } = this.$http.post(
                    'categories',
                    this.addCateForm
                )
                ////////////////////////有一个bug，得不到值 应该是json格式的问题
               // console.log(res)
                // if(res.meta.status!==201){
                //     return this.$message.error('添加分类失败')
                // }
                this.$message.success('添加分类成功')
                this.getCateList()
                this.addCateDialogVisible=false
            })
        },
        parentSelectChange() {
            //console.log(this.selectKeys)
            //选择父级分类
            if (this.selectKeys.length > 0) {
                this.addCateForm.cat_pid = this.selectKeys[
                    this.selectKeys.length - 1
                ]
                this.addCateForm.cat_level = this.selectKeys.length
                return
            } else {
                this.addCateForm.cat_pid = 0
                this.addCateForm.cat_level = 0
                return
            }
        },
        addCateDialogClosed() {
            this.$refs.addCateFormRef.resetFields()
            this.selectKeys = []
            this.addCateForm.cat_level = 0
            this.addCateForm.cat_pid = 0
        },
        showEditDialog(){
            this.$message.info('这个功能和添加类似，先不做了')
        },
        deleteCate(){
            this.$message.info('这个功能和添加类似，先不做了')
        }
    }
}
</script>

<style lang='less' scoped>
.treeTable {
    margin-top: 15px;
}
</style>